import { _decorator, Component, Node, Label, clamp01 } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import { SDKSystem, PlatformType } from '../../Init/SystemSDK/SDKSystem';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { MoveVideoCmp } from '../WinUI/MoveVideoCmp';
import { Adsdk } from '../../myScript/Adsdk';
const { ccclass, property } = _decorator;

@ccclass('LoseUI')
export class LoseUI extends BasicUI {
    @property(Label)
    protected killAwardLabel: Label = null;
    @property(Label)
    protected pathAwardLabel: Label = null;
    @property(Label)
    protected surviveAwardLabel: Label = null;

    @property(Label)
    protected awardLabel: Label = null;
    protected awardNum = 0;
    @property(MoveVideoCmp)
    protected moveVideo: MoveVideoCmp = null;

    public show(d) {
        super.show(d);
        UISystem.hideUI(UIEnum.LevelInfoUI);
        UISystem.showUI(UIEnum.PlayerAssetsUI);
        AudioSystem.playEffect(AudioEnum.Lose);

        this.shareBtn.active = SDKSystem._curPlatform == PlatformType.TTMiniGame ||
            SDKSystem._curPlatform == PlatformType.PCMiniGame;;

        this.setAward();
        this.showAd();

        this.moveVideo.reset();
        this.moveVideo.setData(this.awardNum);

        this.emit(EventTypes.SDKEvents.HideCustomAd);

        this.initMiscontact();

        Adsdk.instance.hideAllAd();
        Adsdk.instance.showInterstitialAd();
        Adsdk.instance.showBanner();
        Adsdk.instance.showGridAdTop();
    }
    @property(Label)
    bossNum: Label = null;
    @property(Label)
    soldierNum: Label = null;
    @property(Label)
    zombieNum: Label = null;
    @property(Label)
    peopleNum: Label = null;

    // #region --------私有----------
    protected setAward() {
        let lv = StorageSystem.getData().levelAssets.curLv;

        const _awardData = GlobalConfig.Award;
        //关卡距离奖励
        let pathAward = Math.floor(Math.abs(GlobalTmpData.Player.wpos3D.z) * _awardData.PathRate);
        this.pathAwardLabel.string = pathAward.toFixed(0);
        //数量
        this.setKillNum();

        const _killData = GlobalTmpData.Player.kill;

        //击杀奖励
        let killAward = Math.floor(_killData.soldierA * _awardData.soldierA +
            _killData.soldierB * _awardData.soldierB +
            _killData.soldierC * _awardData.soldierC +
            _killData.soldierD * _awardData.soldierD +
            _killData.boss * _awardData.boss +
            _killData.people * _awardData.People);

        this.killAwardLabel.string = killAward.toFixed(0);
        // 存活奖励
        let surviveNum = 0;
        this.surviveAwardLabel.string = surviveNum.toFixed(0);

        //总奖励
        this.awardNum = Math.floor(pathAward + killAward);
        this.awardLabel.string = this.awardNum.toFixed(0);
    }

    protected saveAwards(cb) {
        if (this.awardNum > 0) {
            //奖励
            StorageSystem.setData((d) => {
                d.userAssets.asset += Math.round(this.awardNum * 1);
            }, true);
            StorageSystem.updateToAssets(true, true);
        }

        setTimeout(() => {
            cb && cb();
        }, 1500);
    }

    protected onCloseClick() {
        // if (0 == this.clickMislead && GlobalTmpData.isMiscontact && uniSdk.Global.gameConfig) {
        //     ++this.clickMislead; // 用于记录一下是否误触过？

        //     if (uniSdk.Global.gameConfig.resultClickMislead == 1) {
        //         uniSdk.showRewardedVideo(0, (res: number) => { }, this)
        //     }
        //     else if (uniSdk.Global.gameConfig.resultClickMislead == 2) {
        //         uniSdk.showInterstitial();
        //     }
        //     else if (uniSdk.Global.gameConfig.resultClickMislead == 3) {
        //         uniSdk.showCustomAd(0);
        //     }
        //     return;
        // }

        this.saveAwards(() => {
            UISystem.hideUI(UIEnum.LoseUI);
            this.emit(EventTypes.GameEvents.SetLevelManagerEnable, false);
            UISystem.showUI(UIEnum.HomeUI);
        });

        Adsdk.instance.hideAllAd();


        // wx事件
        // wxsdk.GetAdById('LoseAwardClick');
    }

    protected onVideoClick() {
        this.moveVideo.stopMove();
        this.emit(EventTypes.SDKEvents.ShowVideo, {
            success: () => {
                //观看视频成功
                this.awardNum *= this.moveVideo.curAwardRate;
                this.onCloseClick();
            },
            fail: () => {
                this.moveVideo.resumeMove();
            },
            cancel: () => {
                this.moveVideo.resumeMove();
            },
        })
    }

    @property(Node)
    shareBtn: Node = null;

    protected onShareClick() {
        EventManager.emit(EventTypes.SDKEvents.ShareRecord, () => {
            this.shareBtn.active = false;
        });
    }
    //#endregion
    // #region ----------数字动画
    killRecs = {
        soldier: { cur: 0, to: 0, delay: 0 }, people: { cur: 0, to: 0, delay: 0 },
        boss: { cur: 0, to: 0, delay: 0 }, zombie: { cur: 0, to: 0, delay: 0 }
    };
    _curt = 0;
    setKillNum() {
        const _killData = GlobalTmpData.Player.kill;

        this.peopleNum.string = '0';
        this.killRecs.people.cur = 0;
        this.killRecs.people.to = _killData.people;
        this.killRecs.people.delay = 0;

        this.soldierNum.string = '0';
        this.killRecs.soldier.cur = 0;
        this.killRecs.soldier.to = _killData.soldierA + _killData.soldierB + _killData.soldierC + _killData.soldierD;
        this.killRecs.people.delay = 0.3;

        this.bossNum.string = '0';
        this.killRecs.boss.cur = 0;
        this.killRecs.boss.to = _killData.boss;
        this.killRecs.people.delay = 0.6;

        this.zombieNum.string = '0';
        this.killRecs.zombie.cur = 0;
        this.killRecs.zombie.to = GlobalTmpData.Player.survive;
        this.killRecs.people.delay = 0.9;

        this._curt = 0;
    }

    updateKillNum(dt) {
        this._curt += dt;

        if (this._curt >= this.killRecs.soldier.delay) {
            this.killRecs.soldier.cur += (this.killRecs.soldier.to - this.killRecs.soldier.cur) * 0.2 * dt * 60;
        }

        if (this._curt >= this.killRecs.people.delay) {
            this.killRecs.people.cur += (this.killRecs.people.to - this.killRecs.people.cur) * 0.2 * dt * 60;
        }

        if (this._curt >= this.killRecs.boss.delay) {
            this.killRecs.boss.cur += (this.killRecs.boss.to - this.killRecs.boss.cur) * 0.2 * dt * 60;
        }

        if (this._curt >= this.killRecs.zombie.delay) {
            this.killRecs.zombie.cur += (this.killRecs.zombie.to - this.killRecs.zombie.cur) * 0.2 * dt * 60;
        }

        this.peopleNum.string = this.killRecs.people.cur.toFixed(0);
        this.soldierNum.string = this.killRecs.soldier.cur.toFixed(0);
        this.bossNum.string = this.killRecs.boss.cur.toFixed(0);
        this.zombieNum.string = this.killRecs.zombie.cur.toFixed(0);

    }
    // #endregion
    // #region --------广告----------
    @property(Node)
    closeBtn: Node = null;
    @property(Node)
    videoBtn: Node = null;
    _delayCloseBtn = null;

    private showAd() {
        // if (SDKSystem._curPlatform == PlatformType.WXMiniGame) {
        //     this.videoBtn.active = true;
        //     this.closeBtn.active = false;
        //     clearTimeout(this._delayCloseBtn);
        //     this._delayCloseBtn = setTimeout(() => {
        //         this.closeBtn.active = true;
        //     }, 1000);
        // } else {
        // }
        this.videoBtn.active = true;
        this.closeBtn.active = true;
    }

    //#endregion

    //#region ---------误触--------
    clickMislead = 0;
    initMiscontact() {
        this.clickMislead = 0;
    }
    //#endregion

}

